﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Megaman
{
    class Layer
    {
        public List<Texture2D> Textures = new List<Texture2D>();
        public Vector2 ScrollRate;
        private const int maxTextures = 3;
        private char id;


        public Layer(ContentManager content, string basePath,Vector2 scrollRate, char c)
        {

            for (int i = 0; i < maxTextures ; i++)
            {

                try
                {

                    Textures.Add(content.Load<Texture2D>(basePath + "/" + i));
                }
                catch (ContentLoadException)
                {
                    break;
                }
            }
            this.id = c;
            ScrollRate = scrollRate;
        }


        public void Draw(SpriteBatch spriteBatch, Vector2 cameraPosition, Vector2 layerPosition )
        {

            int segmentWidth = Textures[0].Width;
            int segmentHeight = Textures[0].Height;

            int topSegment = (int)Math.Floor(cameraPosition.Y / segmentHeight);
            int bottomSegment = topSegment + 1;

            bool drawn = true;

            if (cameraPosition.X > (layerPosition.X +segmentWidth))
            {
                drawn = false;
            }
            if(cameraPosition.X + segmentWidth < layerPosition.X )
            {
                drawn = false;
            }
            if (cameraPosition.Y > (layerPosition.Y +segmentHeight))
            {
                drawn = false;
            }
            if(cameraPosition.Y + segmentHeight < layerPosition.Y )
            {
                drawn = false;
            }
            
            int layerHeight = (int)Math.Floor(layerPosition.Y / segmentHeight);
            if (drawn && (layerHeight == bottomSegment  || layerHeight == topSegment ))
            {
                float x = cameraPosition.X * ScrollRate.X;
                Rectangle source = new Rectangle(0, 0, segmentWidth, segmentHeight);
                int leftSegment = (int)Math.Floor(x / segmentWidth);
                int rightSegment = leftSegment + 1;
                x = (x / segmentWidth - leftSegment) * -segmentWidth;

                x += layerPosition.X;
                float y = cameraPosition.Y * ScrollRate.Y;

                y = (y / segmentHeight - topSegment) * -segmentHeight;

                Rectangle destinationtopLeft = new Rectangle((int)x, (int)y, segmentWidth, segmentHeight);
                Rectangle destinationtopRight = new Rectangle((int)x + segmentWidth, (int)y, segmentWidth , segmentHeight);
                Rectangle destinationbottomRight = new Rectangle((int)x + segmentWidth, (int)y + segmentHeight, segmentWidth , segmentHeight);
                Rectangle destinationbottomLeft = new Rectangle((int)x, (int)y + segmentHeight, segmentWidth, segmentHeight);

                if (layerHeight == bottomSegment)
                {
                    spriteBatch.Draw(Textures[leftSegment % Textures.Count], destinationbottomLeft, source, Color.White);
                 //   spriteBatch.Draw(Textures[rightSegment % Textures.Count], destinationbottomRight, source, Color.White);

                }
                else
                {
                    spriteBatch.Draw(Textures[leftSegment % Textures.Count], destinationtopLeft, source, Color.White);
                  //  spriteBatch.Draw(Textures[rightSegment % Textures.Count], destinationtopRight, source, Color.White);
                }
            }

        }




    }

}
   